﻿using System;
using System.Linq;
using System.Web.Mvc;
using DealKhuyenMaiV2.com.Models;
using PagedList;
using System.Collections.Generic;
namespace DealKhuyenMaiV2.com.Controllers
{
    [DealKhuyenMaiV2.com.Controllers.AccountController.AdminAuthorize]
    public class ProductManagerController : BaseController
    {
        //
        // GET: /ProductManager/


        public ViewResult Index()
        {
            var products = DbConText.Products.Include("Account").Include("Catalogy").Include("StatusProduct").Where(
                p => (p.StatusProduct.NameStatus == "Đã duyệt" || p.StatusProduct.NameStatus == "Hết hạn"));
            int pageSize = 10;
            ViewBag.Methol = "Index";
            ViewBag.Display = "Index";
            return View(products.ToList().ToPagedList(1, pageSize));
        }
        [HttpPost]
        public PartialViewResult Index(int? page)
        {
            var products = DbConText.Products.Include("Account").Include("Catalogy").Include("StatusProduct").Where(
                p => (p.StatusProduct.NameStatus == "Đã duyệt" || p.StatusProduct.NameStatus == "Hết hạn"));
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Index";
            ViewBag.Display = "Index";
            return PartialView("_ListProduct", products.ToList().ToPagedList(pageNumber, pageSize));
        }
        //
        // GET: /ProductManager/Search?KeyWord=das
        [HttpPost]
        public PartialViewResult Search(int? page, string KeyWord, string Display)
        {
            var products =
                DbConText.Products.Include("Account").Include("Catalogy").Include("StatusProduct").Where(
                    p => ((p.NameProduct.IndexOf(KeyWord) > -1 || KeyWord.IndexOf((string)p.NameProduct) > -1 ||
                          p.Description.IndexOf(KeyWord) > -1 || p.Condition.IndexOf(KeyWord) > -1 ||
                          p.Catalogy.NameCatalogy.IndexOf(KeyWord) > -1 ||
                          KeyWord.IndexOf((string)p.Catalogy.NameCatalogy) > -1
                         )&&(p.StatusProduct.NameStatus == "Đã duyệt" || p.StatusProduct.NameStatus == "Hết hạn")));
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Search";
            ViewBag.Display = Display;
            ViewBag.KeyWord = KeyWord;
            return PartialView("_ListProduct", products.ToList().ToPagedList(pageNumber, pageSize));
        }



        //
        // GET: /ProductManager/Create
        [HttpGet]
        public ActionResult Create()
        {
            ViewBag.IDAccount = new SelectList(DbConText.Accounts, "IDAccount", "Email");
            ViewBag.IDCatalogy = new SelectList(DbConText.Catalogies, "IDCatalogy", "NameCatalogy");
            ViewBag.IDStatus = new SelectList(DbConText.StatusProducts, "IDStatus", "NameStatus");
            ViewBag.IDProvince = new SelectList(DbConText.Provinces, "IDProvince", "NameProvince");
            return View();
        }

        //
        // POST: /ProductManager/Create

        [HttpPost]
        public ActionResult Create(Product product)
        {

            product.DateAdded = DateTime.Now;
            product.IDStatus = 1;

            if (ModelState.IsValid)
            {
                DbConText.Products.AddObject(product);
                DbConText.SaveChanges();
                string numberpicture = Request.Form["NumberofPicture"];
                List<string> listPathImage = new List<string>();
                int numpicture = int.Parse(numberpicture);
                for (int i = 0; i < numpicture; i++)
                {
                    string NameInput = "product_image[" + (i + 1) + "]";
                    string imagesPath = Request.Form[NameInput];
                    if (imagesPath != null)
                        listPathImage.Add(imagesPath);
                }
               
                foreach (string imgPath in listPathImage)
                {
                    Image imageNew = new Image();
                    imageNew.IDProduct = product.IDProduct;
                    imageNew.Path = imgPath;
                    DbConText.Images.AddObject(imageNew);

                }
                DbConText.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.IDAccount = new SelectList(DbConText.Accounts, "IDAccount", "Email", product.IDAccount);
            ViewBag.IDCatalogy = new SelectList(DbConText.Catalogies, "IDCatalogy", "NameCatalogy", product.IDCatalogy);
            ViewBag.IDStatus = new SelectList(DbConText.StatusProducts, "IDStatus", "NameStatus", product.IDStatus);
            ViewBag.IDProvince = new SelectList(DbConText.Provinces, "IDProvince", "NameProvince");
            return View(product);
        }

        //
        // GET: /ProductManager/Edit/5

        public ActionResult Edit(int id)
        {
            Product product = DbConText.Products.Single(p => p.IDProduct == id);
            ViewBag.IDAccount = new SelectList(DbConText.Accounts, "IDAccount", "Email", product.IDAccount);
            ViewBag.IDCatalogy = new SelectList(DbConText.Catalogies, "IDCatalogy", "NameCatalogy",product.IDCatalogy);
            ViewBag.IDStatus = new SelectList(DbConText.StatusProducts, "IDStatus", "NameStatus",product.IDStatus);
            ViewBag.IDProvince = new SelectList(DbConText.Provinces, "IDProvince", "NameProvince",product.IDProvince);
            return View(product);
        }

        //
        // POST: /ProductManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Product product)
        {
            
            Product _product = DbConText.Products.Single(p => p.IDProduct == id);
            {
                _product.NameProduct = product.NameProduct;
                _product.Price = product.Price;
                _product.Discount = product.Discount;
                _product.Date_start = product.Date_start;
                _product.Date_end = product.Date_end;
                _product.IDAccount = product.IDAccount;
                _product.Description = product.Description;
                _product.Shipping = product.Shipping;
                _product.IDStatus = product.IDStatus;
                //_product.DateAdded = product.DateAdded;
                _product.IDCatalogy = product.IDCatalogy;
                _product.SortOrder = product.SortOrder;
                _product.TotalOfNumber = product.TotalOfNumber;
                _product.NumberOfPurchased = product.NumberOfPurchased;
                _product.Condition = product.Condition;
                _product.Summary = product.Summary;
                _product.AllowableNumber = product.AllowableNumber;
                _product.IDProvince = product.IDProvince;
            }

            if (ModelState.IsValid)
            {
                DbConText.SaveChanges();
                string numberpicture = Request.Form["NumberofPicture"];
                List<string> listPathImage = new List<string>();
                int numpicture = int.Parse(numberpicture);
                for (int i = 0; i < numpicture; i++)
                {
                    string NameInput = "product_image[" + (i + 1) + "]";
                    string imagesPath = Request.Form[NameInput];
                    if (imagesPath != null)
                        listPathImage.Add(imagesPath);
                }
                var result = from p in DbConText.Images where p.IDProduct == id select p;
                foreach (Image image in result)
                {
                    DbConText.Images.DeleteObject(image);
                }

                DbConText.SaveChanges();
                foreach (string imgPath in listPathImage)
                {
                    Image imageNew = new Image();
                    imageNew.IDProduct = id;
                    imageNew.Path = imgPath;
                    DbConText.Images.AddObject(imageNew);

                }
                DbConText.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDAccount = new SelectList(DbConText.Accounts, "IDAccount", "Email", product.IDAccount);
            ViewBag.IDCatalogy = new SelectList(DbConText.Catalogies, "IDCatalogy", "NameCatalogy", product.IDCatalogy);
            ViewBag.IDStatus = new SelectList(DbConText.StatusProducts, "IDStatus", "NameStatus", product.IDStatus);
            ViewBag.IDProvince = new SelectList(DbConText.Provinces, "IDProvince", "NameProvince", product.IDProvince);
            return View(product);
        }

     



        //
        // POST: /ProductManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public PartialViewResult DeleteConfirmed(int? page, int id)
        {
            Product product = DbConText.Products.Single(p => p.IDProduct == id);
            product.IDStatus = 4;
            DbConText.SaveChanges();
            var products = DbConText.Products.Include("Account").Include("Catalogy").Include("StatusProduct").Where(
                p => (p.StatusProduct.NameStatus == "Đã duyệt" || p.StatusProduct.NameStatus == "Hết hạn"));
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Index";
            ViewBag.Display = "Index";
            return PartialView("_ListProduct", products.ToList().ToPagedList(pageNumber, pageSize));

        }

        public ViewResult DuyetTinKhuyenMai()
        {
            var products = DbConText.Products.Include("Account").Include("Catalogy").Include("StatusProduct")
                .Where(p => p.StatusProduct.NameStatus == "Chưa duyệt");
            //var products = DbConText.Products.Include("Account").Include("Catalogy").Include("StatusProduct");
            int pageSize = 10;
            ViewBag.Methol = "DuyetTinKhuyenMai";
            ViewBag.Display = "DuyetTinKhuyenMai";
            return View(products.ToList().ToPagedList(1, pageSize));
        }
        [HttpPost]
        public PartialViewResult DuyetTinKhuyenMai(int? page)
        {
            var products = DbConText.Products.Include("Account").Include("Catalogy").Include("StatusProduct")
                .Where(p => p.StatusProduct.NameStatus == "Chưa duyệt");
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "DuyetTinKhuyenMai";
            ViewBag.Display = "DuyetTinKhuyenMai";
            return PartialView("_ListProduct", products.ToList().ToPagedList(pageNumber, pageSize));
        }
        public ViewResult DuyetSanPham(int id)
        {
            Product product = DbConText.Products.Single(p => p.IDProduct == id);
           
            return View(product);
        }

        [HttpPost, ActionName("DuyetSanPham")]
        public ActionResult XacNhanDuyetSanPham(int id)
        {
            Product product = DbConText.Products.Single(p => p.IDProduct == id);

            product.IDStatus = 2;
            DbConText.SaveChanges();
            return RedirectToAction("DuyetTinKhuyenMai");
        }

        protected override void Dispose(bool disposing)
        {
            //DbConText.Dispose();
            //base.Dispose(disposing);
        }
    }
}
